package part1.api.io.mytest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class FOSDemo {
    public static void main(String[] args) {
        //文件流单个写出和读取
        try (
                FileOutputStream fos = new FileOutputStream("fos.txt");
                FileInputStream fis = new FileInputStream("fos.txt");

        ) {
            fos.write(3);
            System.out.println("写出完毕");
            int a = fis.read();
            System.out.println(a);

        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("---------");
        //文件流单个复制
        try (
                FileOutputStream fos = new FileOutputStream("fos1.txt");
                FileInputStream fis = new FileInputStream("fos.txt");

        ) {
            int d;
            while ((d = fis.read()) != -1) {
                fos.write(d);
            }
            System.out.println("单字节拷贝完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }


        System.out.println("------------");

        //文件流按字节数组复制
        File file = new File("下载.jpg");
        try (
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream("下载1.jpg");
        ) {
            byte[] data = new byte[(int) file.length()];
            int len;
            while ((len=fis.read(data)) != -1){
                fos.write(data,0,len);
            }
            System.out.println("字节数组拷贝完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
